package cn.iocoder.yudao.module.member.service.userpackagedetail;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.userpackagedetail.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userpackagedetail.UserPackageDetailDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.userpackagedetail.UserPackageDetailMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 用户套餐购买明细 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserPackageDetailServiceImpl implements UserPackageDetailService {

    @Resource
    private UserPackageDetailMapper userPackageDetailMapper;

    @Override
    public Long createUserPackageDetail(UserPackageDetailSaveReqVO createReqVO) {
        // 插入
        UserPackageDetailDO userPackageDetail = BeanUtils.toBean(createReqVO, UserPackageDetailDO.class);
        userPackageDetailMapper.insert(userPackageDetail);
        // 返回
        return userPackageDetail.getId();
    }

    @Override
    public void updateUserPackageDetail(UserPackageDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateUserPackageDetailExists(updateReqVO.getId());
        // 更新
        UserPackageDetailDO updateObj = BeanUtils.toBean(updateReqVO, UserPackageDetailDO.class);
        userPackageDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserPackageDetail(Long id) {
        // 校验存在
        validateUserPackageDetailExists(id);
        // 删除
        userPackageDetailMapper.deleteById(id);
    }

    private void validateUserPackageDetailExists(Long id) {
        if (userPackageDetailMapper.selectById(id) == null) {
            throw exception(USER_PACKAGE_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public UserPackageDetailDO getUserPackageDetail(Long id) {
        return userPackageDetailMapper.selectById(id);
    }

    @Override
    public PageResult<UserPackageDetailDO> getUserPackageDetailPage(UserPackageDetailPageReqVO pageReqVO) {
        return userPackageDetailMapper.selectPage(pageReqVO);
    }

}